#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "MatrixKey.h"

unsigned char KeyNumber=0;
unsigned int PassWord=0,count=0;

void main()
{
	LCD_Init();
	LCD_ShowString(1,1,"PassWord:");
	while(1)
	{
		KeyNumber=MatrixKey();
		if(KeyNumber)
		{
			if(KeyNumber<=10) //如果S1~S10按下，输入密码
			{
				if(count<4)
				{
					PassWord*=10;
					PassWord+=KeyNumber%10; //获取一位密码
					count++;
				}
				LCD_ShowNum(2,1,PassWord,4); //更新显示
			}
			if(KeyNumber==11)	//按键S11确认密码
			{
				if(PassWord==1234)
				{
					LCD_ShowString(1,12,"OK ");
					PassWord=0;
					count=0;
					LCD_ShowNum(2,1,PassWord,4);
				}
				else
				{
					LCD_ShowString(1,12,"ERR");
					PassWord=0;
					count=0;
					LCD_ShowNum(2,1,PassWord,4);
				}
			}
			if(KeyNumber==12)	//按键S12返回上一次输入
			{
				PassWord=PassWord/10;
				LCD_ShowNum(2,1,PassWord,4);
				if(count>0)
				{
					count--;
				}	
			}
		}
	}
}